package com.freemarker.pickdata;

import java.util.List;
import java.util.Map;

import com.freemarker.db.DataTypeConvertUtil;
import com.freemarker.db.EnvironmentUtil;
import com.freemarker.db.JDBCUtil;
import com.freemarker.db.SpringUtil;
import com.freemarker.vo.TableEntity;

public class DataUtil {

	/**
	 * 生成PO 组装数据
	 */
	public static TableEntity pickPOData(String tableName,String entityPackage) throws Exception {

		List<Map> queryColumnList = JDBCUtil.getListByMap(SpringUtil.getData("queryColumns"), new Object[] { tableName });
		if (queryColumnList == null || queryColumnList.size() == 0) {
			throw new NullPointerException("table [" + tableName + "] not exists");
		}

		Map queryTable = JDBCUtil.getMap(SpringUtil.getData("queryTable"), new Object[] { tableName });
		TableEntity tableEntity = DataTypeConvertUtil.toConvert(queryColumnList, queryTable);
		
		if(tableEntity.getPk()==null){
			throw new NullPointerException("table [" + tableName + "]  PRIMARY KEY  not exists");
		}
		String bussPackage=EnvironmentUtil.getInstance().getPropertyValue("bussPackage");
		tableEntity.setBussPackage(bussPackage);
		tableEntity.setEntityPackage(entityPackage);
		return tableEntity;
	}

}